package com.hxd.otc.module.wallet.presenter;

import com.hxd.otc.base.BaseBean;
import com.hxd.otc.event.Event;
import com.hxd.otc.module.wallet.bean.CoinOneKeyRateVoBean;
import com.hxd.otc.module.wallet.bean.OneKeyCoinTypeRequest;
import com.hxd.otc.module.wallet.bean.WalletCoinRateVoBean;
import com.hxd.otc.module.wallet.contract.WalletCoinChangeContract;
import com.hxd.otc.module.wallet.model.WalletModel;
import com.hxd.otc.mvp.base.BasePresenter;
import com.hxd.otc.net.BaseObserver;

import org.greenrobot.eventbus.EventBus;

import java.util.List;

/**
 * 包  名 :  com.hxd.otc.mvp.wallet
 * 类  名 :  CoinOneKeyChangePresenter
 * 作  者 :  卫广玉
 * 时  间 :  2018/6/5 17:49
 * 描  述 :  一键换币
 */
public class CoinOneKeyChangePresenter extends BasePresenter<WalletCoinChangeContract.View> implements WalletCoinChangeContract.Presenter {

    private final WalletModel mModel;

    public CoinOneKeyChangePresenter(WalletCoinChangeContract.View view) {
        super(view);
        mModel = WalletModel.getInstance();
    }

    @Override
    public void start() {

    }

    /**
     * //获取验证码
     *
     * @param phoneHost
     * @param phoneNumber
     * @param type
     */
    @Override
    public void getSmsCode(String phoneHost, String phoneNumber, String type) {
        mView.setSMSEnable(false);
        mModel.getCode(new BaseObserver() {
            @Override
            public void onNext(String title, Object o) {
                mView.SmsSendSuccess();
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.showToast(errMessage);
                mView.setSMSEnable(true);
            }

        }, mView.<BaseBean>bindAutoDispose(), phoneHost, phoneNumber, type);
    }

    //汇率
    @Override
    public void getWalletCoinRate() {
        mView.onShowLoading();
        mModel.getCoinChangeThanRate(new BaseObserver<List<CoinOneKeyRateVoBean>>() {
            @Override
            public void onNext(String title, List<CoinOneKeyRateVoBean> beans) {
                mView.onHideLoading();
                mView.getWalletCoinRateSuccess(beans);
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.onHideLoading();
                mView.showToast(errMessage);
            }
        }, mView.<BaseBean<List<CoinOneKeyRateVoBean>>>bindAutoDispose());
    }


    /**
     * AEC转MSC
     *
     * @param aecAddress aec钱包地址
     * @param aecAmount  aec数量
     * @param captchCode 验证码
     * @param fundPasswd 交易密码
     * @param mscAddress msc钱包地址
     * @param mscAmount  msc数量
     */
    @Override
    public void AECChangeMSC(String aecAddress, String aecAmount, String captchCode, String fundPasswd, String mscAddress, String mscAmount) {
        //不可重复点击
        mView.setTransferEnable(false);
        mView.onShowLoading();
        mModel.AECChangeMSC(new BaseObserver<BaseBean>() {
            @Override
            public void onNext(String title, BaseBean bean) {
                mView.onHideLoading();
                mView.changeCoinSuccess();
                mView.setTransferEnable(true);
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.onHideLoading();
                mView.showToast(errMessage);
                mView.setTransferEnable(true);
            }
        }, mView.<BaseBean>bindAutoDispose(), aecAddress, aecAmount, captchCode, fundPasswd, mscAddress, mscAmount);
    }

    /**
     * 其他币 转 AEC
     *
     * @param aec2btcAmount BTC折合AEC数量
     * @param aec2ltcAmount LTC折合AEC数量
     * @param aecAddress    aec钱包地址
     * @param btcAddress    btc钱包地址
     * @param btcAmount     btc数量
     * @param captchCode    验证码
     * @param fundPasswd    交易密码
     * @param ltcAddress    ltc钱包地址
     * @param ltcAmount     ltc数量
     */
    @Override
    public void CoinChangeAEC(String aec2btcAmount, String aec2ltcAmount, String aec2ethAmount,
                              String aecAddress,
                              String btcAddress, String btcAmount,
                              String captchCode, String fundPasswd,
                              String ltcAddress, String ltcAmount,
                              String ethAddress, String ethAmount) {
        //不可重复点击
        mView.setTransferEnable(false);
        mView.onShowLoading();
        mModel.CoinChangeAEC(new BaseObserver<BaseBean>() {
            @Override
            public void onNext(String title, BaseBean bean) {
                mView.onHideLoading();
                mView.changeCoinSuccess();
                mView.setTransferEnable(true);
                //钱包首页刷新
                EventBus.getDefault().post(new Event.WalletAccountRefreshEvent());
            }

            @Override
            public void onError(int errType, String errMessage) {
                mView.onHideLoading();
                mView.showToast(errMessage);
                mView.setTransferEnable(true);
            }
        }, mView.<BaseBean>bindAutoDispose(), aec2btcAmount, aec2ltcAmount,aec2ethAmount, aecAddress, btcAddress, btcAmount, captchCode, fundPasswd, ltcAddress, ltcAmount, ethAddress, ethAmount);
    }

    @Override
    public void oneCoinTypeChange(String coinType, String payPassword, List<OneKeyCoinTypeRequest> tradeCoinDtoList) {

            //不可重复点击
            mView.setTransferEnable(false);
            mView.onShowLoading();
            mModel.oneCoinTypeChange(new BaseObserver<BaseBean>() {
                @Override
                public void onNext(String title, BaseBean bean) {
                    mView.onHideLoading();
                    mView.changeCoinSuccess();
                    mView.setTransferEnable(true);
                    //钱包首页刷新
                    EventBus.getDefault().post(new Event.WalletAccountRefreshEvent());
                }

                @Override
                public void onError(int errType, String errMessage) {
                    mView.onHideLoading();
                    mView.showToast(errMessage);
                    mView.setTransferEnable(true);
                }
            }, mView.<BaseBean>bindAutoDispose(), coinType, payPassword,tradeCoinDtoList);
        }

}
